package com.da.javatest.thread.juc.future;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * @author chenlida
 * @date 2020/7/13 10:32
 * @description
 */
public class Future6 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        long startTime = System.currentTimeMillis();
        ExecutorService es = Executors.newFixedThreadPool(5);

        List<Callable<Integer>> list = new ArrayList<>();
        for (int i = 5; i > 0; i--) {
            int j = i * 2;
            String taskName = "Task" + i;
            list.add(() -> {
                TimeUnit.SECONDS.sleep(j);
                System.out.println(taskName + "执行完毕!");
                return j;
            });
        }

        List<Future<Integer>> futures = es.invokeAll(list);
        for (Future<Integer> future : futures) {
            System.out.println("耗时:" + (System.currentTimeMillis() - startTime) + ",执行结果:" + future.get());
        }

        //Integer integer = es.invokeAny(list);
        //System.out.println("耗时:" + (System.currentTimeMillis() - startTime) + ",执行结果:" + integer);
        es.shutdown();
    }
}
